package com.pzl.projection.push;


import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.pzl.projection.push.other.DebugUtil;

// 由于手机系统提示MediaProjection需在前台服务里面使用
public class ScreenDisplayService extends Service implements ScreenDisplayH265Codec.IScreenDisplay {
    final String TAG = "jinx_projection";

    //设备屏幕录屏、截屏系统服务
    MediaProjectionManager mediaProjectionManager;

    NetDispatcher netDispatcher;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "ScreenDisplayService onCreate");

        mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
        netDispatcher = new NetDispatcher(DebugUtil.port);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "ScreenDisplayService onBind");

        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "ScreenDisplayService onStartCommand");

        startForeground (1,createForegroundNotification());

        int resultCode = intent.getIntExtra("resultCode", -1);
        Intent data = intent.getParcelableExtra("data");
        //获取 MediaProjection
        MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);

        //视频数据编码器
        ScreenDisplayH265Codec screenDisplayH265Codec = ScreenDisplayH265Codec.getInstance();
        screenDisplayH265Codec.registerDataReceiver(this);
        screenDisplayH265Codec.init(mediaProjection, 720, 1280);
        screenDisplayH265Codec.start();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
        netDispatcher.close();
        super.onDestroy();
        Log.i(TAG, "ScreenDisplayService onDestroy");
    }

    //前台服务
    private Notification createForegroundNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 唯一的通知通道的id.
        String notificationChannelId = "notification_channel_id_projectionPush";
        // Android8.0以上的系统，新建消息通道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //用户可见的通道名称
            String channelName = "Foreground Service Notification";
            //通道的重要程度
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, importance);
            notificationChannel.setDescription("Channel description");
            //LED灯
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            //震动
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(notificationChannel);
            }
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannelId);
        //通知小图标
        builder.setSmallIcon(R.drawable.ic_launcher_foreground);
        //通知标题
        builder.setContentTitle("同屏Demo");
        //通知内容
        builder.setContentText("同屏Demo");
        //设定通知显示的时间
        builder.setWhen(System.currentTimeMillis());
        //创建通知并返回
        return builder.build();
    }


    //获取屏幕视频录制编码（H265）的每一帧数据
    @Override
    public void onReceiveFrameData(byte[] frameData) {
        Log.i(TAG, "一帧数据 size = " + frameData.length);

        if (netDispatcher != null) netDispatcher.sendData(frameData);
    }

}
